// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const {
  STATUSCODE,
  ModelNameMapper,
  BaseSuccessResponse,
  BaseFailedResponse
} = require('../config.js')

// 云函数入口函数
exports.main = async (event, context) => {
  const {
    api,
  } = event;
  // api部分 格式为 “AAA.bb.ccc”;// 
  if (api == null || api.length == 0) {
    return {
      status: STATUSCODE.NOTFOUND,
      message: 'api路径不对或者不符合规则',
      data: null
    }
  }
  // 
  const path = api.split('.');
  debugger
  if (path.length > 0) {
    const modelName = path[0];
    switch (modelName) {
      case ModelNameMapper.user: {
        const res = await cloud.callFunction({
          name: ModelNameMapper.user,
          data: event
        });
        debugger
        return res.result
      }

      break;
    case ModelNameMapper.login: {
      const res = await cloud.callFunction({
        name: ModelNameMapper.login,
        data: event
      });
      return res.result
    }

    break;
    default:
      return {
        status: STATUSCODE.NOTFOUND,
          message: '找不到对应模块',
          data: null
      }
    }
  } else {
    return {
      status: STATUSCODE.NOTFOUND,
      message: 'api路径不对或者不符合规则',
      data: null
    }
  }










  const wxContext = cloud.getWXContext()

  return {
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}